package com.yunchang.springboot.scheduled.mwsfinance;

import com.yunchang.springboot.mapper.mwsreport.MwsDataGetErrorRecordMapper;
import com.yunchang.springboot.models.mwsreportbean.MwsDataGetErrorRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.yunchang.springboot.utils.DateUtil.getDatesBetweenTwoDate;

/**
 * @author Administrator
 */
@Component
@Configuration
@Slf4j
@EnableScheduling
public class FinanceStartScheduled {

    /**
     * 已接收库存
     */
    private FReceiveScheduled fReceiveScheduled;

    @Autowired
    public void setFReceiveScheduled(FReceiveScheduled fReceiveScheduled){
        this.fReceiveScheduled = fReceiveScheduled;
    }

    /**
     * 出入库明细
     */
    private FInventoryDetailsScheduled fInventoryDetailsScheduled;

    @Autowired
    public void setFInventoryDetailsScheduled(FInventoryDetailsScheduled fInventoryDetailsScheduled){
        this.fInventoryDetailsScheduled = fInventoryDetailsScheduled;
    }

    /**
     * 库存数据
     */
    private FInventoryDailyScheduled fInventoryDailyScheduled;

    @Autowired
    public void setFInventoryDailyScheduled(FInventoryDailyScheduled fInventoryDailyScheduled){
        this.fInventoryDailyScheduled = fInventoryDailyScheduled;
    }

    /**
     * payment销售退款（包含下推数据在途物资）
     */
    private FPaymentRefundScheduled fPaymentRefundScheduled;

    @Autowired
    public void setFPaymentRefundScheduled(FPaymentRefundScheduled fPaymentRefundScheduled){
        this.fPaymentRefundScheduled = fPaymentRefundScheduled;
    }

    /**
     * 亚马逊退货
     */
    private FFbaCustomerReturnScheduled fFbaCustomerReturnScheduled;

    @Autowired
    public void setFFbaCustomerReturnScheduled(FFbaCustomerReturnScheduled fFbaCustomerReturnScheduled){
        this.fFbaCustomerReturnScheduled = fFbaCustomerReturnScheduled;
    }

    /**
     * 赔偿
     */
    private FReimbursementsScheduled fReimbursementsScheduled;

    @Autowired
    public void setFReimbursementsScheduled(FReimbursementsScheduled fReimbursementsScheduled){
        this.fReimbursementsScheduled = fReimbursementsScheduled;
    }

    /**
     * 盘库数据
     */
    private FAdjustmentsScheduled fAdjustmentsScheduled;

    @Autowired
    public void setFAdjustmentsScheduled(FAdjustmentsScheduled fAdjustmentsScheduled){
        this.fAdjustmentsScheduled = fAdjustmentsScheduled;
    }

    /**
     * 已完成订单
     */
    private FFbaShipmentSalesScheduled fFbaShipmentSalesScheduled;

    @Autowired
    public void setFFbaShipmentSalesScheduled(FFbaShipmentSalesScheduled fFbaShipmentSalesScheduled){
        this.fFbaShipmentSalesScheduled = fFbaShipmentSalesScheduled;
    }
    /**
     * 移除货件
     */
    private FRemovalScheduled fRemovalScheduled;

    @Autowired
    public void setFRemovalScheduled(FRemovalScheduled fRemovalScheduled){
        this.fRemovalScheduled = fRemovalScheduled;
    }
    /**
     * 期末盘库
     */
    private FAdjustmentInventoryMonthlyScheduled fAdjustmentInventoryMonthlyScheduled;

    @Autowired
    public void setFAdjustmentInventoryMonthlyScheduled(FAdjustmentInventoryMonthlyScheduled fAdjustmentInventoryMonthlyScheduled){
        this.fAdjustmentInventoryMonthlyScheduled = fAdjustmentInventoryMonthlyScheduled;
    }

    public void fReceiveTest(String date) throws ParseException, CloneNotSupportedException {
        // 生成财务数据
        fReceiveScheduled.getFReceive(date);
        // 生成出入库明细
        fInventoryDetailsScheduled.getFInventoryDetailsByFReceive(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("已接收库存已完成");
    }

    public void fGoodsInTransitTest(String date) throws CloneNotSupportedException {
        // 生成财务数据
        fPaymentRefundScheduled.getFPaymentRefund(date);
        // 生成下推在途物资数据及生成出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFGoodsInTransit(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("在途物资已完成");
    }

    public void fFbaCustomerReturnTest(String date) throws ParseException, CloneNotSupportedException {
        // 生成财务数据
        fFbaCustomerReturnScheduled.getFFbaCustomerReturn(date);
        // 生成出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFFbaCustomerReturn(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("亚马逊退货已完成");
    }

    public void fReimbursementQuantityTest(String date) throws CloneNotSupportedException {
        // 调用赔偿及下推数据生成财务数据
        fReimbursementsScheduled.getFReimbursements(date);
        // 生成出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFReimbursementQuantity(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("赔偿下推已完成");
    }

    public void fAdjustmentsOneTest(String date) throws CloneNotSupportedException {
        List<String> reasonList = Arrays.asList("6", "7", "E", "H", "K", "U", "F", "N", "M", "5", "3", "Q");
        // 调用盘库生成财务数据
        fAdjustmentsScheduled.getFAdjustments(date, reasonList);
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFAdjustments(reasonList, date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("盘库1已完成");
    }

    public void fAdjustmentsTwoTest(String date) throws CloneNotSupportedException {
        List<String> reasonList = Arrays.asList("P");
        // 调用盘库生成财务数据
        fAdjustmentsScheduled.getFAdjustments(date, reasonList);
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFAdjustments(reasonList, date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("盘库2已完成");
    }

    public void fAdjustmentsThreeTest(String date) throws CloneNotSupportedException {
        List<String> reasonList = Arrays.asList("D", "4", "O");
        // 调用盘库生成财务数据
        fAdjustmentsScheduled.getFAdjustments(date, reasonList);
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFAdjustments(reasonList, date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("盘库3已完成");
    }

    public void fFbaShipmentSalesTest(String date) throws CloneNotSupportedException {
        // 调用已完成订单生成财务数据
        fFbaShipmentSalesScheduled.getFFbaShipmentSales(date);
        // 生成出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFFbaShipmentSales(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("已完成订单已完成");
    }

    public void fRemovalTest(String date) throws CloneNotSupportedException {
        // 调用移除货件生成财务数据
        fRemovalScheduled.getFRemoval(date);
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFRemoval(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("移除货件已完成");
    }

    public void fAdjustmentInventoryMonthlyTest(String date) throws CloneNotSupportedException {
        // 调用期末盘库生成财务数据
        fAdjustmentInventoryMonthlyScheduled.getFAdjustmentInventoryMonthlyData(date);
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFAdjustmentInventoryMonthly(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("期末盘库已完成");
    }

    public void financeCaravanDebitCreditRelationTest(String date) {
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFinanceCaravanDebitCreditRelation(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("马帮数据已完成");
    }

    public void fOverseasWarehouseQuantityTest(String date) throws CloneNotSupportedException {
        // 生成对应的出入库明细数据
        fInventoryDetailsScheduled.getFInventoryDetailsByFOverseasWarehouseQuantity(date);
        // 更新库存数据
        fInventoryDailyScheduled.getInventoryDaily(date);
        log.info("海外仓数据已完成");
    }

    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRES_NEW)
    public void mwsFinanceStartByDate(String date) throws ParseException, CloneNotSupportedException {
        log.info("生成mws财务数据的开始时间为：" + date);
        // 根据日期生成新的库存信息
        fInventoryDailyScheduled.insertInventoryByDate(date);
        fReceiveTest(date);
        fGoodsInTransitTest(date);
        fFbaCustomerReturnTest(date);
        fReimbursementQuantityTest(date);
        fAdjustmentsOneTest(date);
        fAdjustmentsTwoTest(date);
        fAdjustmentsThreeTest(date);
        fFbaShipmentSalesTest(date);
        fRemovalTest(date);
        financeCaravanDebitCreditRelationTest(date);
        fOverseasWarehouseQuantityTest(date);
    }
}
